package com.fadadianqi.logistics.admin.controller;

import com.fadadianqi.logistics.admin.controller.form.AjaxResult;
import com.fadadianqi.goods.model.Brand;
import com.fadadianqi.goods.model.Category;
import com.fadadianqi.goods.model.Properties;
import com.fadadianqi.goods.service.GoodsService;
import com.yeqiangwei.commons.util.BeanUtils;
import com.yeqiangwei.commons.util.ParamUtils;
import com.yeqiangwei.commons.util.ValidatorHelper;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;


@Controller("admin_brandCategoryController")
public class BrandCategoryController {



    /**
     * update
     *
     * @param request
     * @param modelMap
     * @return
     * @see com.fadadianqi.logistics.admin.controller.BrandController#ajaxSave(com.fadadianqi.goods.model.Brand, org.springframework.validation.BindingResult, org.springframework.web.bind.support.SessionStatus)
     * @deprecated
     */
    @RequestMapping("/a/brand-category/category/save")
    @ResponseBody
    public ResponseEntity<String> categorySave(HttpServletRequest request, ModelMap modelMap) {
        AjaxResult rs = null;
        String id = ParamUtils.getString(request, "id", null);
        Category category = goodsService.getCategoryById(id);
        category.setName(ParamUtils.getString(request, "name", null));

        goodsService.saveCategory(category);
        rs = new AjaxResult(category, Boolean.TRUE, null);

        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.add("Content-Type", "text/html; charset=utf-8");
        return new ResponseEntity<String>(rs.toJSON(),
                responseHeaders, HttpStatus.OK);
    }

    /**
     * @param request
     * @param modelMap
     * @return
     * @see com.fadadianqi.logistics.admin.controller.BrandController#ajaxSave(com.fadadianqi.goods.model.Brand, org.springframework.validation.BindingResult, org.springframework.web.bind.support.SessionStatus)
     * @deprecated
     */
    @RequestMapping("/a/brand-category/brand/save")
    @ResponseBody
    public ResponseEntity<String> brandSave(HttpServletRequest request, ModelMap modelMap) {
        AjaxResult rs = null;
        String id = ParamUtils.getString(request, "id", null);

        Brand brand = goodsService.getBrandById(id);
        brand.setName(ParamUtils.getString(request, "name", null));

        goodsService.saveBrand(brand);
        rs = new AjaxResult(brand, Boolean.TRUE, null);

        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.add("Content-Type", "text/html; charset=utf-8");
        return new ResponseEntity<String>(rs.toJSON(),
                responseHeaders, HttpStatus.OK);
    }

    /**
     * @param request
     * @param modelMap
     * @return
     * @see com.fadadianqi.logistics.admin.controller.BrandController#showList(javax.servlet.http.HttpServletRequest, org.springframework.ui.ModelMap)
     * @see com.fadadianqi.logistics.admin.controller.CategoryController#showList(javax.servlet.http.HttpServletRequest, org.springframework.ui.ModelMap)
     * @deprecated
     */
    @RequestMapping(value = "/a/brand-category/list", method = RequestMethod.GET)
    public String showList(HttpServletRequest request, ModelMap modelMap) {

        List<Brand> brandList = null;
        List<Category> categoryList = null;

        brandList = goodsService.findAllBrand();
        modelMap.put("brandList", brandList);

        categoryList = goodsService.findAllCategory();
        modelMap.put("categoryList", categoryList);

        List<CategoryVO> categoryVOList = new ArrayList<BrandCategoryController.CategoryVO>();
        if (ValidatorHelper.isNotEmpty(categoryList)) {
            for (Category c : categoryList) {
                CategoryVO vo = new CategoryVO();
                BeanUtils.copyProperties(vo, c);
                List<Properties> props = goodsService.findPropsByCategory(c);
                vo.setProps(props);
                categoryVOList.add(vo);
            }
            modelMap.put("categoryVOList", categoryVOList);
        }
        return "/admin/brand-category/list";
    }

    @Resource(name = "goodsService")
    private GoodsService goodsService;

    public class CategoryVO extends Category {

        /**
         *
         */
        private static final long serialVersionUID = 8162288685519636757L;

        private List<Properties> props;

        public List<Properties> getProps() {
            return props;
        }

        public void setProps(List<Properties> props) {
            this.props = props;
        }
    }
}
